#include "system.h"



#include "bsp_usart1.h"
#define _bsp_put_char(ch) bsp_usart1_put_char(ch)

//加入以下代码,支持printf函数,而不需要选择use MicroLIB
#if 1
#if defined(__CC_ARM)
#pragma import(__use_no_semihosting)
//解决HAL库使用时,某些情况可能报错的bug
int _ttywrch(int ch)
{
    ch = ch;
    return ch;
}
//标准库需要的支持函数
struct __FILE
{
    int handle;
    /* Whatever you require here. If the only file you are using is */
    /* standard output using printf() for debugging, no file handling */
    /* is required. */
};
/* FILE is typedef’ d in stdio.h. */
FILE __stdout;
//定义_sys_exit()以避免使用半主机模式
void _sys_exit(int x)
{
    x = x;
}
//重定义fputc函数
int fputc(int ch, FILE *f)
{
    return _bsp_put_char(ch);
}

#elif defined(__GNUC__)

/* GCC */

int _write(int file, char *ptr, int len)
{
    int idx;
    /*
     * write "len" of char from "ptr" to file id "fd"
     * Return number of char written.
     *
     * Only work for STDOUT, STDIN, and STDERR
     */
    if (file > 2) return -1;

    for (idx = 0; idx < len; idx++)
    {
        _bsp_put_char(ptr[idx]);
    }
    return len;
}

/* Embedded Studio printf() support */
/* IDE setting : Runtime Library = SEGGER , Library I/O = STD */
int __SEGGER_RTL_stdout_putc(int c)
{
    return _bsp_put_char(c);
}

int __SEGGER_RTL_stdout_putc_std(int c)
{
    return _bsp_put_char(c);
}

#endif

#endif
